/*
 * 作者: 侯晓宇
 * 时间: 2017-02-17
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {
	var appInfo = require('app/AppInfo');
	var c = require('app/utils/CommonUtils');
	var EventUtils = require('app/utils/EventUtils');
	var fs = api.require("fs");
	var s = {};


	var _roomuuid = "";
	var _userid = "";
	var _name = "";
	var _headimgurl = "";
	var _chairlist = "";

	var defaultSize = 50;
	var opt = {};
	opt.isplaying = false;
	opt.currentplay = null;
	opt.chattimestamp = new Date().getTime();


	function displayplaying(msgid) {

		if (msgid == null) {
			return;
		}

		var item = $("#audioplay_" + msgid);

		var ppt = item.data("num");
		var data = {};
		data.num = ppt;
		EventUtils.send(api.winName, "goto_ppt", data);

		item.data("playing", "1"); // 标注当前播放状态

		var next_msgid = readnext(msgid);
		console.log("next_msgid:" + next_msgid);
		$("#unread_" + msgid).removeClass("unread");

		///启动定时器
		var t = 3;

		function next() {
			if (item.data("playing") == "0") {
				stopplaying(msgid);
				return;
			}
			item.removeClass("msg-icon-mic-l3");
			item.removeClass("msg-icon-mic-l2");
			item.removeClass("msg-icon-mic-l1");
			item.addClass("msg-icon-mic-l" + (t % 3 + 1));
			t = t + 1;
			setTimeout(next, 500);
		}
		next();

		opt.currentplay = msgid;
		var t_path = 'fs://record/' + msgid + ".amr";
		console.log("t_path:" + t_path);
		
		api.startPlay({
			path: t_path
		}, function(ret, err) {
			c.debug("apiplay_audio:ret", ret);
			c.debug("apiplay_audio:err", err);

			if(c.isNull(ret) || ret.state == 0){//本地没有此文件从远程服务器下载
				var audio_path = c.handleUrl($("#audio_" + msgid).data("newsrc"));
				api.download({
					url: audio_path,
					savePath: t_path,
					report: false,
					cache: false,
					allowResume: true
				}, function(ret, err) {
					
					c.debug("apidownload_audio:ret",ret);
					c.debug("apidownload_audio:err",err);
					var savePath = ret.savePath;
					if (ret.state == 1) {
						//下载成功
						api.startPlay({
							path: savePath
						}, function(ret, err) {
							c.debug("apiplay_audio:ret",ret);
							c.debug("apiplay_audio:err",err);
							c.l(msgid);
							stopplaying(msgid);
							setTimeout(function() {
								displayplaying(next_msgid);
							}, 500);
						});
					}
				});
			}else{
				stopplaying(msgid);
				setTimeout(function() {
					displayplaying(next_msgid);
				}, 500);
			
			}
		});


		//		



		//		
		//		var fs = api.require('fs');
		//		var ret = fs.exist({
		//			path: t_path
		//		},function(ret,err){
		//			c.l(ret);
		//			c.l(err);
		//		});

		
		//		c.debug("record_exist",ret);
		//		if (ret.exist) {
		//			api.startPlay({
		//				path: t_path
		//			}, function(ret, err) {
		//				stopplaying(msgid);
		//				setTimeout(function() {
		//					displayplaying(next_msgid);
		//				}, 500);
		//			});
		//
		//		} else {
		//			//				stopplaying(msgid);
		//			
		//		}

	}

	function readnext(msgid) {

		//当前为unread,下一个也为unread
		if ($("#unread_" + msgid).hasClass("unread")) {
			console.log("true");
			var next_msgid = $("#" + msgid).next().attr("id");
			console.log(next_msgid);
			console.log($("#" + msgid).next().prop("id"));
			if ($("#unread_" + next_msgid).hasClass("unread")) {

				return next_msgid;

			} else {
				console.log("null");
				return null;
			}

		} else {
			console.log("false");
			return null;
		}
	}

	function stopplaying(msgid) {

		$("#audioplay_" + msgid).data("playing", "0");
		$("#audioplay_" + msgid).removeClass("msg-icon-mic-l3");
		$("#audioplay_" + msgid).removeClass("msg-icon-mic-l2");
		$("#audioplay_" + msgid).removeClass("msg-icon-mic-l1");
		$("#audioplay_" + msgid).addClass("msg-icon-mic-l3");
		api.stopPlay();
		opt.currentplay = null;

	}


	function stopallplaying() {

		$(".msg-icon-play").data("playing", "0");
		$(".msg-icon-play").removeClass("msg-icon-mic-l3");
		$(".msg-icon-play").removeClass("msg-icon-mic-l2");
		$(".msg-icon-play").removeClass("msg-icon-mic-l1");
		$(".msg-icon-play").addClass("msg-icon-mic-l3");
		api.stopPlay();
		opt.currentplay = null
	}



	var _ischair = function(userid) {
		for (var i = 0; i < _chairlist.length; i++) {
			if (_chairlist[i].uuid == userid) {
				return true;
			}
		}
		return false;
	}
	s.getCurrentplay_msgid = function() {
		return opt.currentplay;
	}
	s._displayplaying = function(msgid) {
		displayplaying(msgid);
	}
	s._stopplaying = function(msgid) {
		stopplaying(msgid);
	}
	s._stopallplaying = function() {
		stopallplaying();
	}

	return s;
});